﻿using DeviceControl;
using LuaConsole;
using Modbus.Device;
using ServoDrivesLib.Controls;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.AxHost;

namespace 雷赛板卡上位机.Utilities
{
    public static class LuaMasterExtension
    {
        private static Dictionary<string, string> buffer = new Dictionary<string, string>();
        public static LuaMaster lua;
        public static string LuaPath;
        public static void AddClass(this LuaMaster luaMaster)
        {
            luaMaster["CardMaster"] = App.GetService<CardControlManager>().Get(Services.ControlService.GetControlType().ToString());
            luaMaster["GetPortNames"] = SerialPort.GetPortNames;
            luaMaster["ModbusMaster"] = App.GetService<ModbusRtu>();
            luaMaster.AddFunction();

        }

        public static void AddFunction(this LuaMaster luaMaster) 
        {
            luaMaster.Master.RegisterFunction("GetPos", typeof(Sensor).GetMethod("GetPos"));
            luaMaster.Master.RegisterFunction("GetForce", typeof(Sensor).GetMethod("GetForce"));
            luaMaster.Master.RegisterFunction("GetEncoder", typeof(Sensor).GetMethod("GetEncoder"));
            luaMaster.Master.RegisterFunction("SetEncoderZero", typeof(Sensor).GetMethod("SetEncoderZero"));
            luaMaster.Master.RegisterFunction("SetNoOffPower", typeof(Sensor).GetMethod("SetNoOffPower"));
            luaMaster.Master.RegisterFunction("ImportModel", typeof(LuaMasterExtension).GetMethod("Import"));
            luaMaster.Master.RegisterFunction("OpenFileDialog", typeof(OpenFileDialogEx).GetMethod("OpenFileDialog"));
        }

        public static object Import(string model)
        {
            if (lua == null || LuaPath == null) return null;
            if (buffer.TryGetValue(model, out string f))
            {
                return lua.Master.DoFile(f)[0];
            }
            foreach (string file in FindFile(LuaPath))
            {
                var t = Path.GetFileName(file).ToLower();
                if (t.Equals($"{model}.lua".ToLower()))
                {
                    if (!buffer.TryAdd(model, file))
                    {
                        buffer[model] = file;
                    }
                    return lua.Master.DoFile(file)[0];
                }
            }
            throw new Exception($"未找到{model}文件");
        }

        static IEnumerable<string> FindFile(string sSourcePath)
        {
            //遍历文件夹
            DirectoryInfo theFolder = new DirectoryInfo(sSourcePath);

            FileInfo[] thefileInfo = theFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly);

            foreach (FileInfo NextFile in thefileInfo)  //遍历文件

                yield return NextFile.FullName;

            //遍历子文件夹

            DirectoryInfo[] dirInfo = theFolder.GetDirectories();

            foreach (DirectoryInfo NextFolder in dirInfo)

            {
                FileInfo[] fileInfo = NextFolder.GetFiles("*.*", SearchOption.AllDirectories);

                foreach (FileInfo NextFile in fileInfo)  //遍历文件

                    yield return (NextFile.FullName);
            }
        }
    }
}
